package net.gyapp.markim.module.contact;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.easyder.wrapper.base.view.WrapperSwipeActivity;
import com.easyder.wrapper.core.model.BaseVo;

import net.gyapp.markim.ApiConfig;
import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.basic.event.GroupChanged;
import net.gyapp.markim.module.basic.event.GroupIml;
import net.gyapp.markim.utils.RequestParams;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import me.winds.widget.usage.TitleView;

/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/8/4.
 * Desc:
 */

public class GroupNoticeActivity extends WrapperSwipeActivity<CommonPresenter> {

    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;

    String groupNumber;

    public static Intent getIntent(Context context, String groupNumber, String notice, boolean isGroupManager) {
        return new Intent(context, GroupNoticeActivity.class).putExtra("notice", notice)
                .putExtra("groupNumber", groupNumber).putExtra("isGroupManager", isGroupManager);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_group_notice;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setCenterText("群公告");
        groupNumber = intent.getStringExtra("groupNumber");
        btnConfirm.setVisibility(intent.getBooleanExtra("isGroupManager", false) ? View.VISIBLE : View.GONE);
        etContent.setHint(intent.getBooleanExtra("isGroupManager", false) ? "暂无公告" : "请输入公告");
        etContent.setText(intent.getStringExtra("notice"));
        etContent.setEnabled(intent.getBooleanExtra("isGroupManager", false));
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {
        if(url.contains(ApiConfig.API_UPDATE_GROUP_NOTICE)) {
            showToast("设置成功");
            EventBus.getDefault().post(new GroupChanged(GroupIml.SIGN_NOTICE_CHANGED));
            finish();
        }
    }


    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        String content = etContent.getText().toString();
        if (TextUtils.isEmpty(content)) {
            showToast("请输入公告");
            return;
        }
        updateGroupNotice(groupNumber, content);
    }

    /**
     * 更新群通知
     *
     * @param groupNumber
     * @param groupNotice
     */
    private void updateGroupNotice(String groupNumber, String groupNotice) {
        presenter.setNeedDialog(false);
        presenter.postData(ApiConfig.API_UPDATE_GROUP_NOTICE, new RequestParams().put("groupNumber", groupNumber).put("groupNotice", groupNotice).get());
    }
}
